<?php 
$page = array('editTour' => true, 'datePicker' => true, 'prototype' => true);
$this->load->view('layout/adminHeader', $page); ?>
    
    <div id="mainContent" class="floatFix">
    	<?php $this->load->view('layout/adminToursSubmenu'); ?>
        
    	<div class="lCol">
        	        	
        	<?php $this->load->view('layout/flashdata'); ?>
            
           	<h2>Edit Tour</h2>
            
            <?php echo validation_errors(); ?>
        	
            <form action="<?php echo site_url("tours/edit/" . $tour['id']); ?>" method="post" id="editForm" name="editForm">
                <ul class="formContainer">
                	<li class="floatFix">
                        <?php if ($tour['active']) { ?><input type="submit" name="deactivate" id="deactivate" class="submitButton" value="Deactivate This Tour" onclick="return confirm('Are you sure you want to deactivate this tour?')" />
						<?php } else { ?><input type="submit" name="activate" id="activate" class="submitButton" value="Activate This Tour" /><?php } ?>
                    </li>
                
                    <li class="floatFix">
                        <label>Tour Type</label>
                        <?php 
						if ($tour['active']) 
						{
							echo '<p class="additionalFormInfo">' . $tour['tour_name'] . '</p>';
							echo '<input type="hidden" name="tourtype" id="tourtype" value="' . $tour['tourtype_id'] . '" />';
						} else {
							echo tourtypes_dropdown($tourtypes, $tour['tourtype_id']);
                        } ?>
                    </li>
                    
                    <li class="floatFix">
                    	<label>Departure Date</label>
                        <?php 
						if ($tour['active']) 
						{
							echo '<p class="additionalFormInfo">' . natural_date_for_site($tour['departure']) . '</p>';
							echo '<input type="hidden" name="departure_date" id="departure_date" value="' . date_for_site($tour['departure']) . '" />';
						} else {
							?><input type="text" class="w8em format-d-m-y divider-slash range-low-today smallInput" id="departure_date" name="departure_date" value="<?php echo date_for_site($tour['departure']); ?>" /><?php
                        } ?>
                    </li>
                    
                    <li class="floatFix">
                        <label>Price</label>
                        <?php if (!$tour['use_default_price'])
						{
							$p = number_format($tour['custom_price'], 2, '.', ''); 
						} else { 
							$p = '';
						} ?>
                        <input type="text" class="smallInput" name="price" id="price" value="<?php echo $p; ?>" onfocus="uncheckDefault();" />
                        <input type="checkbox" class="checkbox" name="use_default_price" id="use_default_price" <?php if ($tour['use_default_price']) { echo 'checked="checked" '; }?>onclick="clearCustomPrice();" />
                        <p class="additionalFormInfo">Use Default Price (&pound;<?php echo number_format($tour['default_price'], 2, '.', ''); ?>)</p>
                    </li>
                    
                    <li class="floatFix">
                        <label>Special Offer Price?</label>
                        <input type="checkbox" class="checkbox" name="special_offer" id="special_offer" <?php if ($tour['special_offer']) { echo 'checked="checked" '; }?>onclick="uncheckDefault();" />
                    </li>
                    
                    <li class="floatFix">
                    	<input type="submit" class="submitButton" name="save" id="save" value="Save changes" />
                        <?php if (!$tour['active']) { ?><input type="submit" name="saveAndActivate" id="saveAndActivate" class="submitButton" value="Save and Activate This Tour" /><?php } ?>
                    </li>
                    
                    <li>Tour Capacity: <?php echo $tour['capacity']; ?></li>
                    
                    <li>Bookings to date: <?php echo $tour['passengers']; ?></li>
                    
                    <li>Spaces Remaining: <?php echo $tour['capacity'] - $tour['passengers']; ?></li>
                    
                    <?php
					for ($b = 0; $b < count($tour_buses); $b++)
					{
					?>
                    <li id="busField<?php echo $tour_buses[$b]['bus_number']; ?>">
                        <fieldset>
                            <legend>Bus <?php echo $tour_buses[$b]['bus_number']; ?></legend>
                            <input type="hidden" name="buses[<?php echo $b; ?>][bus_number]" id="buses[<?php echo $b; ?>][bus_number]" value="<?php echo $tour_buses[$b]['bus_number']; ?>" />
                            <ul class="formContainer">
                            
                                <li class="floatFix">
                                    <label>Bus Type</label>
                                    <?php echo buses_dropdown($bus_list, $tour_buses[$b]['bus_id'], null, 'buses[' . $b . ']', $tour['active']); ?>
                                </li>
                                
                                <li class="floatFix">
                                    <label>Use default itinerary</label>
                                    <input type="checkbox" class="checkbox" name="buses[<?php echo $b; ?>][default_itinerary]" id="buses[<?php echo $b; ?>][default_itinerary]"<?php if ($tour_buses[$b]['default_itinerary']) { echo ' checked="checked"'; } ?> />
                                </li>
        
                                <?php
								if ($tour_buses[$b]['default_itinerary'])
								{ 
                                    for ($i = 0; $i < count($default_itinerary); $i++)
                                    {
                                        $d = "<li class=\"floatFix\">\n";
                                        $d .= "\t<label>Night " . ($i + 1) . "</label>\n";
                                        
                                        $d .= "\t" . hostel_dropdown($i + 1, $default_itinerary[$i]['hostel_id'], 'buses[' . $b . ']') . "\n";
                                                                          
                                        $d .= "</li>";
                                        
                                        echo $d;
                                    }
								} else
								{
									
									for ($i = 0; $i < count($custom_itineraries[$tour_buses[$b]['bus_number']]); $i++)
                                    {
                                        $d = "<li class=\"floatFix\">\n";
                                        $d .= "\t<label>Night " . ($i + 1) . "</label>\n";
                                        
                                        $d .= "\t" . hostel_dropdown($i + 1, $custom_itineraries[$tour_buses[$b]['bus_number']][$i]['hostel_id'], 'buses[' . $b . ']') . "\n";
                                                                          
                                        $d .= "</li>";
                                        
                                        echo $d;
                                    }
								}
                                ?>
                                
                                <?php if ($b == count($tour_buses) - 1 && $b != 0)
								{ ?>
                                <li class="floatFix">
                                    <a href="#" onclick="$('busField<?php echo $tour_buses[$b]['bus_number']; ?>').remove();">Remove this bus</a>
                                </li>
                                <?php } ?>
                            </ul>
                        </fieldset>
                    </li>
                    <?php } ?>
                    
                    <li id="busField<?php echo $b + 1; ?>">
                    	<?php 
						$url = 'tours/additionalBus/';
						$url .= ($b + 1) . "/";
						$url .= $tour['tourtype_id'];
						?>
                    	<a href="#busField<?php echo $b + 1; ?>" onclick="new Ajax.Updater('busField<?php echo $b + 1; ?>', '<?php echo site_url($url); ?>')">Add Another Bus</a>
                    </li>
                    
                    <li class="floatFix">
                    	<input type="submit" class="submitButton" name="save" id="save" value="Save changes" />
                        <?php if (!$tour['active']) { ?><input type="submit" name="saveAndActivate" id="saveAndActivate" class="submitButton" value="Save and Activate This Tour" /><?php } ?>
                    </li>
                                             
                </ul>
            </form>
        </div>
        
        <div class="rCol">
        	<h2>Sidebar</h2>
        </div>
        
    </div>
    
<?php $this->load->view('layout/adminFooter'); ?>